
//
//  CPJEmoticonGroupModel.swift
//  Sina
//
//  Created by YYY on 2017/8/29.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit

import YYModel

class CPJEmoticonGroupModel: NSObject {

    //表情包组名称
    var groupName: String?
    
    //背景图片
    var bgImageName: String?
    
    //表情包目录，从目录info.plist中，取得表情数据
    var directory: String?
    {
        didSet
        {
            //Info.plist
            guard let path = Bundle.main.path(forResource: "HMEmoticon.bundle", ofType: nil),
                let bundle = Bundle(path: path),
                let plistPath = bundle.path(forResource: "info.plist", ofType: nil, inDirectory: directory),
                let plistArray = NSArray(contentsOfFile: plistPath) as? [[String: String]],
                let modelArray = NSArray.yy_modelArray(with: CPJEmoticonModel.self, json: plistArray) as? [CPJEmoticonModel]
                else
            {
                return
            }
            for model in modelArray
            {
                model.directory = directory
            }

            emoticonsModelArray += modelArray
        }
    }
    
    
    //定义存放表情模型数据的空数组
    //使用懒加载，避免后续的解包问题
    lazy var emoticonsModelArray = [CPJEmoticonModel]()
    
    var numberOfPages: Int
    {
        return (emoticonsModelArray.count - 1)/20 + 1
    }
    
    func emoticons(page: Int) -> [CPJEmoticonModel]
    {
        //每页的数量
        let count = 20
        // 0 20  20
        //0 19 20 25       26
        let location = page * count
        var length = count
        
        //判断数组是否越界
        if location + length > emoticonsModelArray.count
        {
            length = emoticonsModelArray.count - location
        }
        let range = NSRange(location: location, length: length)
        
        //截取子数组
        let subArray = (emoticonsModelArray as NSArray).subarray(with: range) as! [CPJEmoticonModel]
        return subArray
    }
    override var description: String
    {
        return yy_modelDescription()
    }
}
